package com.yb.bussiness.statement;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yb.base.dao.BaseDao;
import com.yb.base.dao.IBaseDao;
import com.yb.domain.goods.TgInformation;
import com.yb.domain.statement.Statement;
import com.yb.util.comm.DateTimeUtil;

public class StatementServiceImpl implements StatementService {
    
    private IBaseDao<Statement, Long> dao;
    
    public StatementServiceImpl() {
        dao = new BaseDao<Statement, Long>(Statement.class);
    }
    
    public void saveOrUpdate(Statement statement) {
        dao.saveOrUpdate(statement);
    }
    
    public Map<String, Object> findAll(String page, String row) {
        String sql = "select * from t_g_statement where flag=0";
        String countSql = "select count(n.id) from t_g_statement n where flag=0";
        if (page != null && row != null) {
            int snum = Integer.parseInt(page) - 1;
            sql += " order by weight desc,id desc limit  " + (snum * Integer.parseInt(row)) + "," + row + " ";
        }
        List<Statement> list = dao.findList(sql);
        int total = 0;
        if (list.size() != 0) {
            total = Integer.parseInt((dao.findObjectArryList(countSql).get(0))[0] + "");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("list", list);
        map.put("total", total);
        return map;
    }
    
    public Statement read(Long id) {
        return dao.read(id);
    }
    
    public Statement read(String flag) {
        String sql = "select * from t_g_statement where flag = " + flag;
        return dao.findList(sql).get(0);
    }
    
    public void modifyGoodsWeight(Long id, Integer weights) {
        String sql = "update t_g_statement set weight = ? where id = ?";
        dao.update(sql, new Object[] {weights, id});
    }

    public boolean delete(String id) {
        boolean isFlag = true;
        String sql = "delete from t_g_statement where id ="+id;
        try {
            dao.delete(sql);
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return isFlag;
    }
    
}
